package com.example.java_chatroom_redis_rabbitmq.controller;

import com.example.java_chatroom_redis_rabbitmq.model.Result;
import com.example.java_chatroom_redis_rabbitmq.model.Session;
import com.example.java_chatroom_redis_rabbitmq.model.User;
import com.example.java_chatroom_redis_rabbitmq.service.SessionService;
import com.example.java_chatroom_redis_rabbitmq.service.TodoService;
import com.example.java_chatroom_redis_rabbitmq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@RequestMapping("/session")
@RestController
public class SessionController {
    @Autowired
    private SessionService sessionService;
    @Autowired
    private UserService userService;
    @Autowired
    private TodoService todoService;
    @RequestMapping("/getSessionList")
    public Result<List<Session>> getSessionList(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        List<Session> sessionList = sessionService.getSessionListByUserId(user.getUserId());
        for (Session session1 : sessionList) {
            Session result = sessionService.getSessionInfoBySessionId(session1.getSessionId());
            User user1 = userService.queryUserByUserId(session1.getFriendId());
            session1.setFriendName(user1.getUserName());
            session1.setFriendImage(user1.getImage());
            session1.setLastMessage(result.getLastMessage());
            session1.setLastTime(result.getLastTime());
            Integer ret = todoService.getTodoCountByUserIdAdnFriendId(user.getUserId(), session1.getSessionId());
            session1.setTodoMessage(ret);
        }
        Collections.sort(sessionList, new Comparator<Session>() {
            @Override
            public int compare(Session o1, Session o2) {
                return o2.getLastTime().compareTo(o1.getLastTime());
            };
        });
        return Result.success(sessionList);
    }

    @Transactional
    @RequestMapping("/createSession")
    public Result<Integer> createSession(Integer friendId,HttpServletRequest request) {
        Session session = new Session();
        Integer ret = sessionService.createSession(session);
        if (ret <= 0) return Result.fail(500,-1,"服务器内部出现错误，请联系管理员！");
        HttpSession session1 = request.getSession(false);
        User user = (User) session1.getAttribute("user");
        Integer userId = user.getUserId();
        ret= sessionService.createSessionUser(session.getSessionId(),userId,friendId);
        if (ret < 2) {
            return Result.fail(500,-1,"服务器内部出现错误，请联系管理员!");
        }
        return Result.success(session.getSessionId());
    }
}
